﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Pitaya_IDE
{
    public class MenuPanel
    {
        public readonly menu_bar menu_form ;  //菜单栏

        List<MenuLabel> MenuLabelList = new List<MenuLabel>();

        /*上侧栏按钮高亮处理*/
        public Label label_now = null;
        public Label label_old = null;
        public bool menu_form_vis = false;

        Panel panel;
        Panel Ctrl_panel;
        Label CloseLabel,MaxLabel,MinLabel;
        Label TitleLabel;
        Form1 form1;

        public MenuPanel(Panel _panel, Form1 s_Form)
        {
            panel = _panel;
            menu_form = new menu_bar();  //菜单栏
            MenuLabel tmp;
            form1 = s_Form;

            TitleLabel = new Label()
            {
                Name = "TitleLabel",
                Text = "Pitaya-IDE",
                TextAlign = ContentAlignment.MiddleCenter,
            };

            panel.Controls.Add(TitleLabel);

            Ctrl_panel = new Panel()
            {
                Name = "Ctrl",
                Location = new Point(panel.Width - 80 - 79 -79, 0),
                Size = new Size(79 * 3, 50),
                BackColor = panel.BackColor,
            };

            panel.Controls.Add(Ctrl_panel);

            tmp = new MenuLabel(this)
            {
                Name = "1文件(F)",
                Text = "文件(F)",
                Location = new Point(63, 0),
            };
            MenuLabelList.Add(tmp);
            panel.Controls.Add(tmp);

            tmp = new MenuLabel(this)
            {
                Name = "2编辑(E)",
                Text = "编辑(E)",
                Location = new Point(MenuLabelList[MenuLabelList.Count - 1].Left + 100, 0),
            };
            MenuLabelList.Add(tmp);
            panel.Controls.Add(tmp);

            tmp = new MenuLabel(this)
            {
                Name = "3选择(S)",
                Text = "选择(S)",
                Location = new Point(MenuLabelList[MenuLabelList.Count - 1].Left + 100, 0),
            };
            MenuLabelList.Add(tmp);
            panel.Controls.Add(tmp);

            tmp = new MenuLabel(this)
            {
                Name = "4查看(V)",
                Text = "查看(V)",
                Location = new Point(MenuLabelList[MenuLabelList.Count - 1].Left + 100, 0),
            };
            MenuLabelList.Add(tmp);
            panel.Controls.Add(tmp);

            tmp = new MenuLabel(this)
            {
                Name = "5转到(G)",
                Text = "转到(G)",
                Location = new Point(MenuLabelList[MenuLabelList.Count - 1].Left + 100, 0),
            };
            MenuLabelList.Add(tmp);
            panel.Controls.Add(tmp);

            tmp = new MenuLabel(this)
            {
                Name = "6运行(R)",
                Text = "运行(R)",
                Location = new Point(MenuLabelList[MenuLabelList.Count - 1].Left + 100, 0),
            };
            MenuLabelList.Add(tmp);
            panel.Controls.Add(tmp);

            tmp = new MenuLabel(this)
            {
                Name = "7串口(T)",
                Text = "串口(T)",
                Location = new Point(MenuLabelList[MenuLabelList.Count - 1].Left + 100, 0),
            };
            MenuLabelList.Add(tmp);
            panel.Controls.Add(tmp);

            tmp = new MenuLabel(this)
            {
                Name = "8帮助(H)",
                Text = "帮助(H)",
                Location = new Point(MenuLabelList[MenuLabelList.Count - 1].Left + 100, 0),
            };
            MenuLabelList.Add(tmp);
            panel.Controls.Add(tmp);

            Size LabelSize = new Size(100, panel.Height - 1);
            Color LabelBackColor = Color.FromArgb(13, 17, 23);
            Color LabelForeColor = Color.FromArgb(115, 124, 133);
            Font LabelFont = new Font("等线", 18, FontStyle.Regular);
            ContentAlignment LabelTextAlign = ContentAlignment.MiddleCenter;

            foreach (MenuLabel Ctmp in MenuLabelList)
            {
                Ctmp.Size = LabelSize;
                Ctmp.BackColor = LabelBackColor;
                Ctmp.ForeColor = LabelForeColor;
                Ctmp.Font = LabelFont;
                Ctmp.TextAlign = LabelTextAlign;
            }

            TitleLabel.BackColor = Color.Blue;
            TitleLabel.BackColor = LabelBackColor;
            TitleLabel.ForeColor = LabelForeColor;
            TitleLabel.Font = LabelFont;
            TitleLabel.AutoEllipsis = true;

            Label _tmp = new Label()
            {
                Name = "9",
                Text = "",
                Location = new Point(0, panel.Height - 1),
                Size = new Size(1920, 1),
                BackColor = Color.FromArgb(48, 54, 61),
            };

            panel.Controls.Add(_tmp);


            CloseLabel = new Label()
            {
                Name = "Close",
                Text = "",
                Location = new Point(79 * 2, 0),
                Size = new Size(79, 50),
                BackColor = panel.BackColor,
            };

            Ctrl_panel.Controls.Add(CloseLabel);

            MaxLabel = new Label()
            {
                Name = "Max",
                Text = "",
                Location = new Point(79, 0),
                Size = new Size(79, 50),
                BackColor = panel.BackColor,
            };

            Ctrl_panel.Controls.Add(MaxLabel);

            MinLabel = new Label()
            {
                Name = "Min",
                Text = "",
                Location = new Point(0, 0),
                Size = new Size(79, 50),
                BackColor = panel.BackColor,
            };

            Ctrl_panel.Controls.Add(MinLabel);



            CloseLabel.MouseUp += new MouseEventHandler(MenuLabelClose_MouseUp);
            CloseLabel.MouseEnter += new EventHandler(MenuLabelClose_MouseEnter);
            CloseLabel.MouseLeave += new EventHandler(MenuLabelClose_MouseLeave);

            MaxLabel.MouseUp += new MouseEventHandler(MenuLabelMax_MouseUp);
            MaxLabel.MouseEnter += new EventHandler(MenuLabelMax_MouseEnter);
            MaxLabel.MouseLeave += new EventHandler(MenuLabelMax_MouseLeave);

            MinLabel.MouseUp += new MouseEventHandler(MenuLabelMin_MouseUp);
            MinLabel.MouseEnter += new EventHandler(MenuLabelMin_MouseEnter);
            MinLabel.MouseLeave += new EventHandler(MenuLabelMin_MouseLeave);

            TitleLabel.MouseDown += new MouseEventHandler(MoveLabel_MouseDown);
            TitleLabel.MouseUp += new MouseEventHandler(MoveLabelMax_MouseUp);
            TitleLabel.MouseMove += new MouseEventHandler(MoveLabel_MouseMove);

            panel.MouseDown += new MouseEventHandler(MoveLabel_MouseDown);
            panel.MouseUp += new MouseEventHandler(MoveLabelMax_MouseUp);
            panel.MouseMove += new MouseEventHandler(MoveLabel_MouseMove);
        }

        public void Rd_Label()
        {
            bool flag = false;
            MenuLabel old_tmp = MenuLabelList[0];

            Size tmpSize = new Size((int)(((double)panel.Width - 238 - 63) / 5), 30);
            Point tmpPoint = new Point(panel.Width - 238 - tmpSize.Width, 10);

            int dat = panel.Width - 238 - tmpSize.Width;

            foreach (MenuLabel tmp in MenuLabelList)
            {
                if (tmp.Left + tmp.Width > dat)
                {
                    tmp.Visible = false;
                    if (!flag)
                    {
                        flag = true;
                    }
                }
                else
                {
                    tmp.Visible = true;
                }
                old_tmp = tmp;
            }


            if (!flag && MenuLabelList.Count > 1)
            {
                tmpPoint = new Point(old_tmp.Left + old_tmp.Width, 0);
                tmpSize = new Size(panel.Width - 238 - (old_tmp.Left + old_tmp.Width), 50);
            }

            TitleLabel.Location = tmpPoint;
            TitleLabel.Size = tmpSize;

            Ctrl_panel.Location = new Point(panel.Width - 238, 0);
        }

        public void Set_backcolor(Color color)
        {
            panel.BackColor = color;
            foreach (MenuLabel tmp in MenuLabelList)
            {
                tmp.Backcolor = color;
            }

            MinLabel.BackColor = color;
            MaxLabel.BackColor = color;
            CloseLabel.BackColor = color;
            Ctrl_panel.BackColor = color;
            TitleLabel.BackColor = color;

            Fd_Min();
            Fd_Max();
            Fd_Close();
        }

        public void Fd_Min()
        {
            if (CloseLabel.BackColor.R == 1)
            {
                MinLabel.Image = AllImage.MinMode1;
            }
            else
            {
                MinLabel.Image = AllImage.MinMode2;
            }
        }
        private void MenuLabelMin_MouseUp(object sender, MouseEventArgs e)
        {
            form1.WindowState = FormWindowState.Minimized;
        }
        private void MenuLabelMin_MouseLeave(object sender, EventArgs e)
        {
            Fd_Min();
        }
        private void MenuLabelMin_MouseEnter(object sender, EventArgs e)
        {
            MinLabel.Image = AllImage.MinMode3;
        }

        public void Fd_Max()
        {
            if (form1.WindowState == FormWindowState.Maximized)
            {
                if (CloseLabel.BackColor.R == 1)
                    MaxLabel.Image = AllImage.MaxMode4;
                else MaxLabel.Image = AllImage.MaxMode5;
            }
            else
            {
                if (CloseLabel.BackColor.R == 1)
                    MaxLabel.Image = AllImage.MaxMode1;
                else MaxLabel.Image = AllImage.MaxMode2;
            }
        }
        private void MenuLabelMax_MouseUp(object sender, MouseEventArgs e)
        {
            if (form1.WindowState == FormWindowState.Maximized)
                 form1.WindowState = FormWindowState.Normal;
            else form1.WindowState = FormWindowState.Maximized;

            if (form1.WindowState == FormWindowState.Normal)
            {
                if (CloseLabel.BackColor.R == 1)
                     MaxLabel.Image = AllImage.MaxMode1;
                else MaxLabel.Image = AllImage.MaxMode2;
            }
            else
            {
                if (CloseLabel.BackColor.R == 1)
                     MaxLabel.Image = AllImage.MaxMode4;
                else MaxLabel.Image = AllImage.MaxMode5;
            }
            form1.FrameRefresh(FormWindowState.Normal == form1.WindowState);
            form1.mainPanel.RefLct();//刷新主面板
        }
        private void MenuLabelMax_MouseLeave(object sender, EventArgs e)
        {
            Fd_Max();
        }
        private void MenuLabelMax_MouseEnter(object sender, EventArgs e)
        {
            if (form1.WindowState == FormWindowState.Normal)
            {
                MaxLabel.Image = AllImage.MaxMode3;
            }
            else
            {
                MaxLabel.Image = AllImage.MaxMode6;
            }
        }

        public void Fd_Close()
        {
            if (CloseLabel.BackColor.R == 1)
            {
                CloseLabel.Image = AllImage.ExitMode1;
            }
            else
            {
                CloseLabel.Image = AllImage.ExitMode2;
            }
        }
        private void MenuLabelClose_MouseUp(object sender, MouseEventArgs e) => FormClosing(null);
        private void MenuLabelClose_MouseLeave(object sender, EventArgs e)
        {
            Fd_Close();
        }
        private void MenuLabelClose_MouseEnter(object sender, EventArgs e)
        {
            CloseLabel.Image = AllImage.ExitMode3;
        }

        bool MoveFlag = false;
        Point PussPoint;
        Point WinPoint;
        private void MoveLabel_MouseDown(object sender, MouseEventArgs e)
        {
            WinPoint = form1.Location;
            PussPoint = Control.MousePosition;
            MoveFlag = true;
        }
        private void MoveLabelMax_MouseUp(object sender, MouseEventArgs e)
        {
            MoveFlag = false;
        }
        private void MoveLabel_MouseMove(object sender, MouseEventArgs e)
        {
            if (MoveFlag)
            {
                Point NowPoint = Control.MousePosition;
                form1.Location = new Point(
                    WinPoint.X + (NowPoint.X - PussPoint.X),
                    WinPoint.Y + (NowPoint.Y - PussPoint.Y));


            }
        }

        public bool save_flag = false;  /*退出提示*/
        public void FormClosing(FormClosingEventArgs e)
        {
            DialogResult a = DialogResult.No;
            if (save_flag)
            {
                a = MessageBox.Show("是否保存更改", "Pitaya-IDE", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
            }

            if (e != null)
            {
                if (a == DialogResult.Yes)
                {
                    /*保存文件*/
                    e.Cancel = false;
                    Environment.Exit(0);
                }
                else if (a == DialogResult.No)
                {
                    e.Cancel = false;
                    Environment.Exit(0);
                }
                else if (a == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }
            }
            else
            {
                if (a == DialogResult.Yes)
                {
                    /*保存文件*/
                    Environment.Exit(0);
                }
                else if (a == DialogResult.No)
                {
                    Environment.Exit(0);
                }
                else if (a == DialogResult.Cancel)
                {
                    
                }
            }
        }

    }


    class MenuLabel : Label 
    {
        MenuPanel upMenuPanel;

        Color backcolor;

        public MenuLabel(MenuPanel _upMenuPanel)
        {
            this.MouseDown += new MouseEventHandler(MenuLabel_MouseDown);
            this.MouseMove += new MouseEventHandler(MenuLabel_MouseMove);
            this.MouseEnter += new EventHandler(MenuLabel_MouseEnter);
            this.MouseLeave += new EventHandler(MenuLabel_MouseLeave);

            upMenuPanel = _upMenuPanel;

            backcolor = Color.FromArgb(13,17,23);
        }

        public Color Backcolor
        {
            set {
                backcolor = value;
                BackColor = backcolor;
            }
        }

        //private bool MouseDown_cnt = false;
        private void MenuLabel_MouseDown(object sender, MouseEventArgs e)
        {
            //if (menu_form_vis == 0)
            //{
            //    Label label = (Label)sender;
            //    label_old = label_now;
            //    label_now = label;

            //    Point screenPoint = Control.MousePosition;//鼠标相对于屏幕左上角的坐标
            //    Point labelPoint = label.PointToClient(Control.MousePosition); //鼠标相对于button1左上角的坐标

            //    menu_form.Top = screenPoint.Y - labelPoint.Y + label.Height;
            //    menu_form.Left = screenPoint.X - labelPoint.X;
            //    menu_form.Show();
            //    if (MouseDown_cnt == 0)
            //    {
            //        Point screenPointA = Control.MousePosition;//鼠标相对于屏幕左上角的坐标
            //        Point labelPointA = label.PointToClient(Control.MousePosition); //鼠标相对于button1左上角的坐标

            //        menu_form.Top = screenPointA.Y - labelPointA.Y + label.Height;
            //        menu_form.Left = screenPointA.X - labelPointA.X;
            //        menu_form.Show();
            //        MouseDown_cnt = 1;
            //    }

            //    menu_form_vis = 1;
            //}
            //else
            //{
            //    menu_form.Hide();
            //    menu_form_vis = 0;
            //}
        }

        private void MenuLabel_MouseLeave(object sender, EventArgs e) 
            =>BackColor = backcolor;
        private void MenuLabel_MouseEnter(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            label.BackColor = Color.FromArgb(38, 41, 47);
            if (upMenuPanel.menu_form_vis)
            {
                //label_old = label_now;
                //label_now = this;

                //Point screenPoint = Control.MousePosition;//鼠标相对于屏幕左上角的坐标
                //Point labelPoint = label.PointToClient(Control.MousePosition); //鼠标相对于button1左上角的坐标

                //menu_form.Top = screenPoint.Y - labelPoint.Y + label.Height;
                //menu_form.Left = screenPoint.X - labelPoint.X;
                //menu_form.Show();
            }
        }

        private void MenuLabel_MouseMove(object sender, MouseEventArgs e)
        {
            BackColor = Color.FromArgb(38, 41, 47);
        }
    }
}